package com.start.workflow.core.dispatcher;

import com.start.common.std.StartEventDispatcherListener;
import com.start.common.std.StartSingletonFactory;
import com.start.common.tools.SpringContextTools;
import com.start.workflow.core.event.StartWorkFlowTaskEvent;
import com.start.workflow.entity.StartActBusinessEntity;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Slf4j
public class StartGlobalActTaskEventDispatcher extends StartEventDispatcherListener<StartGlobalActTaskEventDispatcher, StartWorkFlowTaskEvent> implements StartWorkFlowTaskEvent {


    public static StartGlobalActTaskEventDispatcher getInstance() {
        return StartSingletonFactory.get(StartGlobalActTaskEventDispatcher.class);
    }
    /**
     * 初始化分发器
     */
    @Override
    protected void initDispatcher() {
        Map<String, StartWorkFlowTaskEvent> beans = SpringContextTools.getBeans(StartWorkFlowTaskEvent.class, false);
        if (beans == null || beans.isEmpty()) {
            return;
        }
        Collection<StartWorkFlowTaskEvent> values = beans.values();
        for (StartWorkFlowTaskEvent listener : values) {
            if (StringUtils.isNotBlank(listener.key())) {
                addEvent(UNION_KEY_PREFIX,listener.key(),listener);
                continue;
            }
            if (StringUtils.isNotBlank(listener.categoryKey())) {
                addEvent(UNION_KEY_PREFIX,listener.categoryKey(),listener);
                continue;
            }
            addEvent(GLOBAL_PREFIX, GLOBAL_KEY, listener);
        }
    }


    /**
     * 任务创建前
     * modelKey 流程模型key
     * modelCategoryKey 流程模型类别
     *
     * @param actBusiness
     * @param task
     */
    @Override
    public void onTaskCreateBefore(StartActBusinessEntity actBusiness, Task task) {
        List<StartWorkFlowTaskEvent> events = getEvents(UNION_KEY_PREFIX, actBusiness.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCreateBefore(actBusiness, task);
            }
        }
 
        events = getEvents(GROUP_PREFIX, actBusiness.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCreateBefore(actBusiness, task);
            }
        }
       events = getEvents(GLOBAL_PREFIX,GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCreateBefore(actBusiness, task);
            }
        }
    }

    /**
     * 任务创建后
     *
     * @param actBusiness
     * @param task
     */
    @Override
    public void onTaskCreateAfter(StartActBusinessEntity actBusiness, Task task) {
         List<StartWorkFlowTaskEvent> events = getEvents(UNION_KEY_PREFIX, actBusiness.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCreateAfter(actBusiness, task);
            }
        }
        events.clear();
             events = getEvents(GROUP_PREFIX, actBusiness.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCreateAfter(actBusiness, task);
            }
        }
       events = getEvents(GLOBAL_PREFIX,GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCreateAfter(actBusiness, task);
            }
        }
    }

    /**
     * 任务完成
     *
     * @param actBusiness
     * @param task
     */
    @Override
    public void onTaskCompleteBefore(StartActBusinessEntity actBusiness, Task task) {
         List<StartWorkFlowTaskEvent> events = getEvents(UNION_KEY_PREFIX, actBusiness.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCompleteBefore(actBusiness, task);
            }
        }
        events.clear();
             events = getEvents(GROUP_PREFIX, actBusiness.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCompleteBefore(actBusiness, task);
            }
        }
       events = getEvents(GLOBAL_PREFIX,GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCompleteBefore(actBusiness, task);
            }
        }
    }

    /**
     * 任务完成后
     *
     * @param actBusiness
     * @param task
     */
    @Override
    public void onTaskCompleteAfter(StartActBusinessEntity actBusiness, Task task) {
         List<StartWorkFlowTaskEvent> events = getEvents(UNION_KEY_PREFIX, actBusiness.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCompleteAfter(actBusiness, task);
            }
        }
        events.clear();
             events = getEvents(GROUP_PREFIX, actBusiness.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCompleteAfter(actBusiness, task);
            }
        }
       events = getEvents(GLOBAL_PREFIX,GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskCompleteAfter(actBusiness, task);
            }
        }


    }

    /**
     * 任务驳回
     *
     * @param actBusiness
     * @param task
     */
    @Override
    public void onTaskRejectBefore(StartActBusinessEntity actBusiness, Task task) {
         List<StartWorkFlowTaskEvent> events = getEvents(UNION_KEY_PREFIX, actBusiness.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskRejectBefore(actBusiness, task);
            }
        }
        events.clear();
             events = getEvents(GROUP_PREFIX, actBusiness.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskRejectBefore(actBusiness, task);
            }
        }
       events = getEvents(GLOBAL_PREFIX,GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskRejectBefore(actBusiness, task);
            }
        }
    }

    /**
     * 任务驳回后
     *
     * @param actBusiness
     * @param task
     */
    @Override
    public void onTaskRejectAfter(StartActBusinessEntity actBusiness, Task task) {
         List<StartWorkFlowTaskEvent> events = getEvents(UNION_KEY_PREFIX, actBusiness.getProcDefKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskRejectAfter(actBusiness, task);
            }
        }
        events.clear();
             events = getEvents(GROUP_PREFIX, actBusiness.getProcDefCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskRejectAfter(actBusiness, task);
            }
        }
       events = getEvents(GLOBAL_PREFIX,GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowTaskEvent event : events) {
                event.onTaskRejectAfter(actBusiness, task);
            }
        }
    }

}
